<script setup>
import { useUserStore } from "~/store/useUser";
import defaultAvatar from "@/assets/app-mj.png";
import { computed, ref } from "vue";

const props = defineProps({
  msg: {
    type: Object,
    default: {},
  },
});
const userStore = useUserStore();
const router = useRouter();

// 头像
const headSrc = computed(() => {
  if (isMine.value && userStore.getUserInfo.avatar) {
    return userStore.getUserInfo.avatar;
  }
  if (!isMine.value && props.msg.fromAvatar) {
    return props.msg.fromAvatar;
  }
  return defaultAvatar;
});
// 是不是自己的消息
const isMine = computed(() => {
  return props.msg.from === userStore.getUserInfo.uimKey;
});

//
const checkGoods = () => {
  const id = props.msg.exData.id;
  router.push(`/details?id=${id}`);
};
</script>

<template>
  <div class="text-msg">
    <el-image v-if="!isMine" class="head" :src="headSrc" alt="">
      <template #error>
        <div class="image-error">
          <img class="head" :src="defaultAvatar" alt="" />
        </div>
      </template>
    </el-image>
    <div class="msg-text" :class="{ mine: isMine }">
      <p class="name" v-if="!isMine">{{ props.msg.fromName }}</p>
      <div class="goods" :class="{ mine: isMine }" v-if="props.msg.exData">
        <div class="goods-item" @click="checkGoods">
          <img :src="props.msg.exData.coverPic" alt="" />
          <div class="goods-info">
            <p class="name">
              {{ props.msg.exData.goodsName }}
              <span v-if="props.msg.exData.versionType"
                >({{ props.msg.exData.versionType === 1 ? "标准版" : "体验版" }})
              </span>
            </p>
            <span> <span>¥</span>{{ props.msg.exData.goodsAmount }}</span>
          </div>
        </div>
      </div>
      <i v-if="isMine">{{ props.msg.read ? "已读" : "未读" }}</i>
    </div>
    <el-image v-if="isMine" class="head" :src="headSrc" alt="">
      <template #error>
        <div class="image-error">
          <img class="head" :src="defaultAvatar" alt="" />
        </div>
      </template>
    </el-image>
  </div>
</template>

<style scoped lang="scss">
.text-msg {
  display: flex;
  margin-bottom: 26px;
  .head {
    flex: 0 0 40px;
    width: 40px;
    height: 40px;
    margin-right: 8px;
    border-radius: 50%;
  }
  .msg-text {
    width: 100%;
    &.mine {
      text-align: right;
      padding-right: 10px;
      .text {
        background: rgba(1, 154, 129, 1);
        color: #fff;
        border-radius: 12px 0 12px 12px;
      }
      i {
        display: block;
        font-style: normal;
        text-align: right;
        font-size: 12px;
        color: #999;
        padding-right: 10px;
      }
    }
    .name {
      color: rgba(25, 30, 40, 0.36);
      font-size: 12px;
      margin-bottom: 4px;
    }
    .text {
      display: inline-block;
      background: #fff;
      padding: 12px 20px;
      border-radius: 0 12px 12px 12px;
      max-width: 80%;
      box-shadow: 0 4px 12px -2px rgba(11, 13, 18, 0.06);
    }
  }
  .goods {
    display: flex;
    justify-content: flex-start;
    &.mine {
      justify-content: flex-end;
      padding-right: 10px;
      .goods-item {
        // background: rgba(1, 154, 129, 1);
        color: #fff;
        border-radius: 12px 0 12px 12px;
      }
    }
    .goods-item {
      cursor: pointer;
      width: 380px;
      border-radius: 12px 12px 12px 12px;
      background: #ffffff;
      border: 1px solid rgba(25, 30, 40, 0.08);
      display: flex;
      align-items: center;
      padding: 16px;
      box-sizing: border-box;
      img {
        width: 120px;
        height: 76px;
        margin-right: 12px;
        border-radius: 8px;
      }
      .goods-info {
        text-align: left;
        .name {
          font-size: 15px;
          font-weight: bold;
          color: #191e28;
          margin-bottom: 4px;
          line-height: 24px;
          // 超出两行显示省略号
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
          span {
            color: #191e28;
            font-size: 12px;
            font-weight: 300;
          }
        }
        span {
          color: #ff4f4f;
          font-size: 16px;
          font-weight: bold;
          span {
            font-size: 12px;
          }
        }
      }
    }
  }
}
</style>
